/**
 * 
 */
package com.mywind.service.windfarm.impl;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mywind.dao.windfarm.DiagnoseDao;
import com.mywind.dao.windfarm.WindFarmDao;
import com.mywind.entity.windfarm.DiagnoseEntity;
import com.mywind.entity.windfarm.WindFarmEntity;
import com.mywind.service.windfarm.IDiagnoseServ;

/**
 * @author a01513
 *
 */
@Service
public class DiagnoseServ implements IDiagnoseServ {
	
	@Autowired
	private WindFarmDao windfarmDao;
	@Autowired
	private DiagnoseDao diagnoseDao;
	
	@Override
	public List<WindFarmEntity> getAllWindFarm() {
		
		List<WindFarmEntity> list = windfarmDao.findAllWindFarm();
		String basePath = "/diagnose/";		
		
		for(WindFarmEntity wfe : list){
			
			String farmDir = basePath + wfe.getFarmid();
			File file = new File(farmDir);
			if(file.isDirectory()){
				Vector<String> fileNames = new Vector<String>();
				for(File f: file.listFiles()){
					if(f.isDirectory()){
						fileNames.add(f.getName());
					}
				}
				String[] nameArray = new String[fileNames.size()];
				int i = 0;
				for(String name:fileNames){
					nameArray[i++] = name;
				}
				wfe.setTurbine(nameArray);
			}
			
		}
		
		return list;
	}

	@Override
	public boolean setETLStatus(String farmid, int status) {
		windfarmDao.updateETLStatus(farmid, status);
		return true;
	}

	@Override
	public boolean addAllDRecord(List<DiagnoseEntity> list) {
		System.out.println("Total record:" + list.size());
		for(DiagnoseEntity de: list){
//			System.out.println("fcode:" + de.getFcode() + "\n" +
//					"wcode:" + de.getWcode() + "\n" +
//					"trigkey:" + de.getTrigkey() + "\n" +
//					"desc:" + de.getDesc() + "\n" +
//					"times:" + de.getTimes() + "\n" +
//					"msecond:" + de.getMsecond() + "\n" +
//					"gpower:" + de.getGpower() + "\n" +
//					"wspeed:" + de.getWspeed() + "\n" +
//					"gspeed:" + de.getGspeed() + "\n" +
//					"pangle:" + de.getPangle() + "\n");
			if(diagnoseDao.queryRecord(de.getFcode(), de.getWcode(), de.getTrigkey(), de.getTimes(), de.getMsecond())!=null){
				System.out.println("exsit and skip~");
				continue;
			}
			diagnoseDao.insertRecord(de);
		}
		return true;
	}

	@Override
	public boolean updateMtime(String farmid, String mtime) {
		SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
		try {
			windfarmDao.updateMtime(farmid, date.parse(mtime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public WindFarmEntity getWindFarmByFarmid(String farmid) {
		return windfarmDao.findWindFarmByFarmid(farmid);
	}

	@Override
	public boolean deleteRecordByFWcode(String fcode, int wcode) {
		diagnoseDao.deleteRecordByFWcode(fcode, wcode);
		return true;
	}

	@Override
	public List<DiagnoseEntity> getDrecordByFTD(String fcode, int wcode,
			String start, String end) {
		String startDate = start.substring(6, 10) + "-" + start.substring(0, 2) + "-" + start.substring(3, 5) + " 00:00:00";
		String endDate = end.substring(6, 10) + "-" + end.substring(0, 2) + "-" + end.substring(3, 5) + " 23:59:59";
		System.out.println("fcode:" + fcode + "wcode:" + wcode + "start:" + startDate + ",end:" + endDate);
		List<DiagnoseEntity> list = diagnoseDao.findRecordByFTD(fcode, wcode, startDate, endDate);
		List<WindFarmEntity> windfarms = windfarmDao.findAllWindFarm();
		for(DiagnoseEntity de: list)
			for(WindFarmEntity wfe: windfarms){
				if(de.getFcode().equals(wfe.getFarmid()))
					de.setFarmname(wfe.getFarmname());
			}
		System.out.println("recordSize:" + list.size());
		return list;
	}

	@Override
	public List<String> getTurbineList(String farmid) {
		String basePath = "/diagnose/" + farmid + "/";
		File farmDir = new File(basePath);
		List<String> turbine = new ArrayList<>();
		System.out.println("DirPath:" + basePath);
		for(File turbineDir:farmDir.listFiles())
			if(turbineDir.isDirectory()){
				turbine.add(turbineDir.getName());
			}
		return turbine;
	}
	
}
